package api

import (
	"api_gateway/basic/config"
	chat "api_gateway/basic/proto/chat"
	userpb "api_gateway/basic/proto/user"
	request "api_gateway/handler/request/user"
	"api_gateway/handler/response"
	"context"
	"fmt"
	"log"
	"net/http"

	"github.com/gin-gonic/gin"
)

// 关注用户
func UserFollow(c *gin.Context) {
	// 接收参数
	var req request.UserFollowReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数接收失败", err.Error())
		return
	}
	// 调用服务层
	resp, err := config.ChatClient.UserFollow(c, &chat.UserFollowReq{
		FormUserId: uint64(req.FormUserId),
		ToUserId:   uint64(req.ToTargetId),
	})
	if err != nil {
		response.Error(c, http.StatusInternalServerError, "服务端调用失败", err.Error())
		return
	}

	// 发送关注通知
	go func() {
		if err := SendFollowNotification(uint(req.FormUserId), uint(req.ToTargetId), true); err != nil {
			log.Printf("发送关注通知失败: %v", err)
			// 这里可以记录到日志或保存到离线通知，但不影响主流程
		} else {
			log.Printf("关注通知发送成功: 用户%d -> 用户%d", req.FormUserId, req.ToTargetId)
		}
	}()

	// 返回结果
	response.Success(c, resp)
}

// 取消关注用户
func UserNotFollow(c *gin.Context) {
	// 接收参数
	var req request.UserNotFollowReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数接收失败", err.Error())
		return
	}
	// 调用服务层
	resp, err := config.ChatClient.UserNotFollow(c, &chat.UserNotFollowReq{
		FormUserId: uint64(req.FormUserId),
		ToUserId:   uint64(req.ToTargetId),
	})
	if err != nil {
		response.Error(c, http.StatusInternalServerError, "服务端调用失败", err.Error())
		return
	}

	go func() {
		if err := SendFollowNotification(uint(req.FormUserId), uint(req.ToTargetId), false); err != nil {
			log.Printf("发送取消关注通知失败: %v", err)
		} else {
			log.Printf("取消关注通知发送成功: 用户%d -> 用户%d", req.FormUserId, req.ToTargetId)
		}
	}()

	// 返回结果
	response.Success(c, resp)
}

// 发送好友申请
func Application(c *gin.Context) {
	// 接受参数
	var req request.ApplicationReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数接收失败", err.Error())
		return
	}
	// 业务逻辑
	resp, err := config.ChatClient.Application(c, &chat.ApplicationReq{
		FormUserId: uint64(req.FormUserId),
		ToUserId:   uint64(req.ToTargetId),
		Greeting:   req.Greeting,
	})
	if err != nil {
		response.Error(c, http.StatusInternalServerError, "服务层调用失败", err.Error())
		return
	}
	// 发送好友申请通知
	go func() {
		if err := SendFriendRequestNotification(uint(req.FormUserId), uint(req.ToTargetId), req.Greeting); err != nil {
			log.Printf("发送好友申请通知失败: %v", err)
		} else {
			log.Printf("好友申请通知发送成功: 用户%d -> 用户%d", req.FormUserId, req.ToTargetId)
		}
	}()

	// 返回结果
	response.Success(c, resp)
}

// 同意好友申请
func AcceptApplication(c *gin.Context) {
	// 接收参数
	var req request.AcceptApplicationReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数接收失败", err.Error())
		return
	}
	// 调用服务层
	resp, err := config.ChatClient.AcceptApplication(c, &chat.AcceptApplicationReq{
		UserId:        uint64(req.UserId),
		ApplicationId: uint64(req.ApplicationId),
	})
	if err != nil {
		response.Error(c, http.StatusInternalServerError, "服务层调用失败", err.Error())
		return
	}

	// 发送好友申请结果通知
	go func() {
		if err := SendFriendRequestResultNotification(uint(req.UserId), uint(req.ApplicationId), true); err != nil {
			log.Printf("发送好友申请结果通知失败: %v", err)
		} else {
			log.Printf("好友申请结果通知发送成功: 用户%d -> 用户%d", req.UserId, req.ApplicationId)
		}
	}()

	// 返回结果
	response.Success(c, resp)
}

// 拒绝好友申请
func RejectApplication(c *gin.Context) {
	// 接收参数
	var req request.RejectApplicationReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数接收失败", err.Error())
		return
	}
	// 调用服务层
	resp, err := config.ChatClient.RejectApplication(c, &chat.RejectApplicationReq{
		UserId:        uint64(req.UserId),
		ApplicationId: uint64(req.ApplicationId),
	})
	if err != nil {
		response.Error(c, http.StatusInternalServerError, "服务层调用失败", err.Error())
		return
	}

	// 发送好友申请结果通知
	go func() {
		if err := SendFriendRequestResultNotification(uint(req.UserId), uint(req.ApplicationId), false); err != nil {
			log.Printf("发送好友申请结果通知失败: %v", err)
		} else {
			log.Printf("好友申请结果通知发送成功: 用户%d -> 用户%d", req.UserId, req.ApplicationId)
		}
	}()

	// 返回结果
	response.Success(c, resp)
}

// 聊天室推荐
func ChatRoomRecommend(c *gin.Context) {
	// 接收参数
	var req request.ChatRoomRecommendReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, http.StatusBadRequest, "参数接收失败", err.Error())
		return
	}
	// 调用服务层
	resp, err := config.ChatClient.ChatRoomRecommend(c, &chat.ChatRoomRecommendReq{
		UserId:   uint64(req.UserId),
		Category: req.Category,
	})
	if err != nil {
		response.Error(c, http.StatusInternalServerError, "服务层调用失败", err.Error())
		return
	}
	// 返回结果
	response.Success(c, resp)
}

// 获取兴趣标签列表
// @Summary      获取兴趣标签列表
// @Description  获取兴趣标签列表
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        data   query      request.GetInterestTags  true  "获取兴趣标签列表参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Router       /user/interestTags [get]
func GetInterestTags(c *gin.Context) {
	var req request.GetInterestTags
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  fmt.Sprintf("参数错误: %v", err.Error()),
		})
		return
	}

	// 调用用户服务获取兴趣标签列表
	var resp, err = config.UserClient.GetInterestTags(context.Background(), &userpb.GetInterestTagsReq{})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  fmt.Sprintf("获取兴趣标签列表失败: %v", err.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "获取兴趣标签列表成功",
		"data": gin.H{
			"tags": resp.Tags,
		},
	})
}

// 保存用户兴趣选择
// @Summary      保存用户兴趣选择
// @Description  保存用户兴趣选择
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        data   body      request.SaveUserInterests  true  "保存用户兴趣选择参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Router       /user/interests [post]
func SaveUserInterests(c *gin.Context) {
	var req request.SaveUserInterests
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  fmt.Sprintf("参数错误: %v", err.Error()),
		})
		return
	}

	// 调用用户服务保存用户兴趣选择
	var resp, err = config.UserClient.SaveUserInterests(context.Background(), &userpb.SaveUserInterestsReq{
		UserId: req.UserId,
		TagIds: req.TagIds,
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  fmt.Sprintf("保存用户兴趣选择失败: %v", err.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "保存用户兴趣选择成功",
		"data": gin.H{
			"success": resp.Success,
			"message": resp.Message,
		},
	})
}

// 获取推荐用户列表
// @Summary      获取推荐用户列表
// @Description  获取推荐用户列表
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        data   query      request.GetRecommendations  true  "获取推荐用户列表参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Router       /user/recommendations [get]
func GetRecommendations(c *gin.Context) {
	var req request.GetRecommendations
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  fmt.Sprintf("参数错误: %v", err.Error()),
		})
		return
	}

	// 调用用户服务获取推荐用户列表
	var resp, err = config.UserClient.GetRecommendations(context.Background(), &userpb.GetRecommendationsReq{
		UserId:   req.UserId,
		Page:     req.Page,
		PageSize: req.PageSize,
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  fmt.Sprintf("获取推荐用户列表失败: %v", err.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "保存用户兴趣选择成功",
		"data": gin.H{
			"users":     resp.Users,
			"total":     resp.Total,
			"page":      req.Page,
			"page_size": req.PageSize,
			"message":   resp.Message,
		},
	})
}

// 获取智能推荐用户列表
// @Summary      获取智能推荐用户列表
// @Description  获取智能推荐用户列表
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        data   query      request.GetSmartRecommendations  true  "获取智能推荐用户列表参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Router       /user/smartRecommendations [get]
func GetSmartRecommendations(c *gin.Context) {
	var req request.GetSmartRecommendations
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  fmt.Sprintf("参数错误: %v", err.Error()),
		})
		return
	}

	// 调用用户服务获取推荐用户列表
	var resp, err = config.UserClient.GetSmartRecommendations(context.Background(), &userpb.GetSmartRecommendationsReq{
		UserId: req.UserId,
		Limit:  req.Limit,
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  fmt.Sprintf("获取推荐用户列表失败: %v", err.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "保存用户兴趣选择成功",
		"data": resp,
	})
}

// 滑动操作（喜欢/无感）
// @Summary      滑动操作
// @Description  滑动操作（喜欢/无感）
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        data   body      request.SwipeUser  true  "滑动操作参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Router       /user/swipe [post]
func SwipeUser(c *gin.Context) {
	var req request.SwipeUser
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  fmt.Sprintf("参数错误: %v", err.Error()),
		})
		return
	}

	// 调用用户服务滑动操作
	var resp, err = config.UserClient.SwipeUser(context.Background(), &userpb.SwipeUserReq{
		UserId:       req.UserId,
		TargetUserId: req.TargetUserId,
		Action:       req.Action,
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  fmt.Sprintf("滑动操作失败: %v", err.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "滑动操作成功",
		"data": gin.H{
			"message":  resp.Message,
			"is_match": resp.IsMatch,
			"success":  resp.Success,
		},
	})
}

// 获取匹配列表
// @Summary      获取匹配列表
// @Description  获取匹配列表
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        data   query      request.GetMatches  true  "获取匹配列表参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Router       /user/matches [get]
func GetMatches(c *gin.Context) {
	var req request.GetMatches
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  fmt.Sprintf("参数错误: %v", err.Error()),
		})
		return
	}

	// 调用用户服务获取匹配列表
	var resp, err = config.UserClient.GetMatches(context.Background(), &userpb.GetMatchesReq{
		UserId:   req.UserId,
		Page:     req.Page,
		PageSize: req.PageSize,
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  fmt.Sprintf("获取匹配列表失败: %v", err.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "获取匹配列表成功",
		"data": gin.H{
			"matches":   resp.Matches,
			"total":     resp.Total,
			"page":      req.Page,
			"page_size": req.PageSize,
			"message":   resp.Message,
		},
	})
}

// 获取匹配详情
// @Summary      获取匹配详情
// @Description  获取匹配详情
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        data   query      request.GetMatchDetail  true  "获取匹配详情参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Router       /user/match/detail [get]
func GetMatchDetail(c *gin.Context) {
	var req request.GetMatchDetail
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  fmt.Sprintf("参数错误: %v", err.Error()),
		})
		return
	}

	// 调用用户服务获取匹配详情
	var resp, err = config.UserClient.GetMatchDetail(context.Background(), &userpb.GetMatchDetailReq{
		UserId:  req.UserId,  // 发起匹配的用户ID
		MatchId: req.MatchId, // 匹配ID
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  fmt.Sprintf("获取匹配详情失败: %v", err.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "获取匹配详情成功",
		"data": gin.H{
			"interests":  resp.Interests,
			"match_id":   resp.MatchId,
			"user_id":    resp.UserId,
			"nickname":   resp.Nickname,
			"avatar":     resp.Avatar,
			"bio":        resp.Bio,
			"match_time": resp.MatchTime,
			"message":    resp.Message,
		},
	})
}

// 发送聊天消息
// @Summary      发送聊天消息
// @Description  发送聊天消息
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        data   body      request.SendMessage  true  "发送聊天消息参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Router       /user/message/send [post]
func SendMessage(c *gin.Context) {
	var req request.SendMessage
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  fmt.Sprintf("参数错误: %v", err.Error()),
		})
		return
	}

	// 调用用户服务发送聊天消息
	var resp, err = config.UserClient.SendMessage(context.Background(), &userpb.SendMessageReq{
		UserId:  req.UserId,  // 发送者ID
		MatchId: req.MatchId, // 匹配ID
		Content: req.Content, // 消息内容
		MsgType: req.MsgType, // 消息类型 1:文本 2:图片 3:语音
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  fmt.Sprintf("发送聊天消息失败: %v", err.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "发送聊天消息成功",
		"data": gin.H{
			"message_id": resp.MessageId,
			"success":    resp.Success,
			"message":    resp.Message,
		},
	})
}

// 获取聊天消息列表
// @Summary      获取聊天消息列表
// @Description  获取聊天消息列表
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        data   query      request.GetChatMessages  true  "获取聊天消息列表参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Router       /user/messages [get]
func GetChatMessages(c *gin.Context) {
	var req request.GetChatMessages
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  fmt.Sprintf("参数错误: %v", err.Error()),
		})
		return
	}

	// 调用用户服务获取聊天消息列表
	var resp, err = config.UserClient.GetChatMessages(context.Background(), &userpb.GetChatMessagesReq{
		UserId:   req.UserId,   // 用户ID
		MatchId:  req.MatchId,  // 匹配ID
		Page:     req.Page,     // 页码
		PageSize: req.PageSize, // 每页数量
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  fmt.Sprintf("获取聊天消息列表失败: %v", err.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "获取聊天消息列表成功",
		"data": gin.H{
			"messages":  resp.Messages,
			"total":     resp.Total,
			"page":      req.Page,
			"page_size": req.PageSize,
			"message":   resp.Message,
		},
	})
}

// 标记消息为已读
// @Summary      标记消息为已读
// @Description  标记聊天消息为已读
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        data   body      request.MarkMessagesAsRead  true  "标记消息为已读参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Router       /user/messages/read [post]
func MarkMessagesAsRead(c *gin.Context) {
	var req request.MarkMessagesAsRead
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  fmt.Sprintf("参数错误: %v", err.Error()),
		})
		return
	}

	// 调用用户服务标记消息为已读
	var resp, err = config.UserClient.MarkMessagesAsRead(context.Background(), &userpb.MarkMessagesAsReadReq{
		UserId:  req.UserId,  // 用户ID
		MatchId: req.MatchId, // 匹配ID
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  fmt.Sprintf("标记消息为已读失败: %v", err.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "标记消息为已读成功",
		"data": gin.H{
			"success": resp.Success,
			"message": resp.Message,
		},
	})
}

// 获取未读消息数量
// @Summary      获取未读消息数量
// @Description  获取用户未读消息数量
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        data   query      request.GetUnreadMessageCount  true  "获取未读消息数量参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Router       /user/messages/unreadCount [get]
func GetUnreadMessageCount(c *gin.Context) {
	var req request.GetUnreadMessageCount
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  fmt.Sprintf("参数错误: %v", err.Error()),
		})
		return
	}

	// 调用用户服务获取未读消息数量
	var resp, err = config.UserClient.GetUnreadMessageCount(context.Background(), &userpb.GetUnreadMessageCountReq{
		UserId: req.UserId, // 用户ID
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  fmt.Sprintf("获取未读消息数量失败: %v", err.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "获取未读消息数量成功",
		"data": gin.H{
			"count":   resp.Count,
			"message": resp.Message,
		},
	})
}

// 获取用户通知列表
// @Summary      获取用户通知列表
// @Description  获取用户通知列表
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        data   query      request.GetNotifications  true  "获取用户通知列表参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Router       /user/notifications [get]
func GetNotifications(c *gin.Context) {
	var req request.GetNotifications
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  fmt.Sprintf("参数错误: %v", err.Error()),
		})
		return
	}

	// 调用用户服务获取用户通知列表
	var resp, err = config.UserClient.GetNotifications(context.Background(), &userpb.GetNotificationsReq{
		UserId:   req.UserId,   // 用户ID
		Page:     req.Page,     // 页码
		PageSize: req.PageSize, // 每页数量
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  fmt.Sprintf("获取用户通知列表失败: %v", err.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "获取用户通知列表成功",
		"data": gin.H{
			"notifications": resp.Notifications,
			"total":         resp.Total,
			"page":          req.Page,
			"page_size":     req.PageSize,
			"message":       resp.Message,
		},
	})
}

// 标记通知为已读
// @Summary      标记通知为已读
// @Description  标记用户通知为已读
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        data   body      request.MarkNotificationAsRead  true  "标记通知为已读参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Router       /user/notification/read [post]
func MarkNotificationAsRead(c *gin.Context) {
	var req request.MarkNotificationAsRead
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  fmt.Sprintf("参数错误: %v", err.Error()),
		})
		return
	}

	// 调用用户服务标记通知为已读
	var resp, err = config.UserClient.MarkNotificationAsRead(context.Background(), &userpb.MarkNotificationAsReadReq{
		UserId:         req.UserId,         // 用户ID
		NotificationId: req.NotificationId, // 通知ID
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  fmt.Sprintf("标记通知为已读失败: %v", err.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "标记通知为已读成功",
		"data": gin.H{
			"success": resp.Success,
			"message": resp.Message,
		},
	})
}

// 获取灵魂问题列表
// @Summary      获取灵魂问题列表
// @Description  获取用户灵魂问题列表
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        data   body      request.GetSoulQuestions  true  "获取灵魂问题列表参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Router       /user/soulQuestions [get]
func GetSoulQuestions(c *gin.Context) {
	var req request.GetSoulQuestions
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  fmt.Sprintf("参数错误: %v", err.Error()),
		})
		return
	}

	// 调用用户服务标记通知为已读
	var resp, err = config.UserClient.GetSoulQuestions(context.Background(), &userpb.GetSoulQuestionsReq{
		UserId:   req.UserId,
		Category: req.Category,
		Limit:    req.Limit,
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  fmt.Sprintf("获取灵魂问题列表失败: %v", err.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "获取灵魂问题列表成功",
		"data": resp,
	})
}

// 提交灵魂回答
// @Summary      提交灵魂回答
// @Description  提交用户灵魂问题回答
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        data   body      request.SubmitSoulAnswer  true  "提交灵魂回答参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Router       /user/soulQuestions [post]
func SubmitSoulAnswer(c *gin.Context) {
	var req request.SubmitSoulAnswer
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  fmt.Sprintf("参数错误: %v", err.Error()),
		})
		return
	}

	// 调用用户服务标记通知为已读
	var resp, err = config.UserClient.SubmitSoulAnswer(context.Background(), &userpb.SubmitSoulAnswerReq{
		UserId:      req.UserId,
		QuestionId:  req.QuestionId,
		Answers:     req.Answers,
		AnswerScore: req.AnswerScore,
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  fmt.Sprintf("提交灵魂回答失败: %v", err.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "提交灵魂回答成功",
		"data": resp,
	})
}

// 获取灵魂匹配列表
// @Summary      获取灵魂匹配列表
// @Description  获取用户灵魂问题匹配列表
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        data   body      request.GetSoulMatches  true  "获取灵魂匹配列表参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Router       /user/soulMatches [get]
func GetSoulMatches(c *gin.Context) {
	var req request.GetSoulMatches
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  fmt.Sprintf("参数错误: %v", err.Error()),
		})
		return
	}

	// 调用用户服务标记通知为已读
	var resp, err = config.UserClient.GetSoulMatches(context.Background(), &userpb.GetSoulMatchesReq{
		UserId:   req.UserId,
		Page:     req.Page,
		PageSize: req.PageSize,
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  fmt.Sprintf("获取灵魂匹配列表失败: %v", err.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "获取灵魂匹配列表成功",
		"data": resp,
	})
}

// 获取灵魂匹配详情
// @Summary      获取灵魂匹配详情
// @Description  获取用户灵魂问题匹配详情
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        data   body      request.GetSoulMatchDetail  true  "获取灵魂匹配详情参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Router       /user/soulMatchDetail [get]
func GetSoulMatchDetail(c *gin.Context) {
	var req request.GetSoulMatchDetail
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  fmt.Sprintf("参数错误: %v", err.Error()),
		})
		return
	}

	// 调用用户服务标记通知为已读
	var resp, err = config.UserClient.GetSoulMatchDetail(context.Background(), &userpb.GetSoulMatchDetailReq{
		UserId:       req.UserId,
		TargetUserId: req.TargetUserId,
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  fmt.Sprintf("获取灵魂匹配详情失败: %v", err.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "获取灵魂匹配详情成功",
		"data": resp,
	})
}

// 获取用户灵魂问题匹配详情
// @Summary      获取用户灵魂问题匹配详情
// @Description  获取用户灵魂问题匹配详情
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        data   body      request.GetUserSoulProfile  true  "获取用户灵魂问题匹配详情参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Router       /user/soulProfile [get]
func GetUserSoulProfile(c *gin.Context) {
	var req request.GetUserSoulProfile
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  fmt.Sprintf("参数错误: %v", err.Error()),
		})
		return
	}

	// 调用用户服务标记通知为已读
	var resp, err = config.UserClient.GetUserSoulProfile(context.Background(), &userpb.GetUserSoulProfileReq{
		UserId: req.UserId,
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  fmt.Sprintf("获取用户灵魂问题匹配详情失败: %v", err.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "获取用户灵魂问题匹配详情成功",
		"data": resp,
	})
}

// 计算用户灵魂问题匹配度
// @Summary      计算用户灵魂问题匹配度
// @Description  计算用户灵魂问题匹配度
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        data   body      request.CalculateSoulCompatibility  true  "计算用户灵魂问题匹配度参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Router       /user/calculateSoulCompatibility [post]
func CalculateSoulCompatibility(c *gin.Context) {
	var req request.CalculateSoulCompatibility
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  fmt.Sprintf("参数错误: %v", err.Error()),
		})
		return
	}

	// 调用用户服务标记通知为已读
	var resp, err = config.UserClient.CalculateSoulCompatibility(context.Background(), &userpb.CalculateSoulCompatibilityReq{
		UserId1: req.UserId1,
		UserId2: req.UserId2,
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  fmt.Sprintf("计算用户灵魂问题匹配度失败: %v", err.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "计算用户灵魂问题匹配度成功",
		"data": resp,
	})
}

// 更新用户位置
// @Summary      更新用户位置
// @Description  更新用户位置
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        data   body      request.UpdateLocation  true  "更新用户位置参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Router       /user/updateLocation [post]
func UpdateLocation(c *gin.Context) {
	var req request.UpdateLocation
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  fmt.Sprintf("参数错误: %v", err.Error()),
		})
		return
	}

	// 调用用户服务标记通知为已读
	var resp, err = config.UserClient.UpdateLocation(context.Background(), &userpb.UpdateLocationReq{
		UserId:    req.UserId,
		Latitude:  req.Latitude,
		Longitude: req.Longitude,
		City:      req.City,
		Address:   req.Address,
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  fmt.Sprintf("更新用户位置失败: %v", err.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "更新用户位置成功",
		"data": resp,
	})
}

// 获取附近用户
// @Summary      获取附近用户
// @Description  获取附近用户
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        data   body      request.GetNearbyUsers  true  "获取附近用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Router       /user/getNearbyUsers [post]
func GetNearbyUsers(c *gin.Context) {
	var req request.GetNearbyUsers
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  fmt.Sprintf("参数错误: %v", err.Error()),
		})
		return
	}

	// 调用用户服务标记通知为已读
	var resp, err = config.UserClient.GetNearbyUsers(context.Background(), &userpb.GetNearbyUsersReq{
		UserId:    req.UserId,
		Latitude:  req.Latitude,
		Longitude: req.Longitude,
		Radius:    req.Radius,
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  fmt.Sprintf("获取附近用户失败: %v", err.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "获取附近用户成功",
		"data": resp,
	})
}

// 获取用户位置
// @Summary      获取用户位置
// @Description  获取用户位置
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        data   body      request.GetUserLocation  true  "获取用户位置参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Router       /user/getUserLocation [post]
func GetUserLocation(c *gin.Context) {
	var req request.GetUserLocation
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  fmt.Sprintf("参数错误: %v", err.Error()),
		})
		return
	}

	// 调用用户服务标记通知为已读
	var resp, err = config.UserClient.GetUserLocation(context.Background(), &userpb.GetUserLocationReq{
		UserId: req.UserId,
	})

	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  fmt.Sprintf("获取用户位置失败: %v", err.Error()),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "获取用户位置成功",
		"data": resp,
	})
}

// 发表好友动态
func PostFriendActivity(c *gin.Context) {
	var req request.PostFriendActivity
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": http.StatusBadRequest, "msg": fmt.Sprintf("参数错误: %v", err.Error())})
		return
	}
	resp, err := config.UserClient.PostFriendActivity(context.Background(), &userpb.PostFriendActivityReq{UserId: req.UserId, Content: req.Content, Images: req.Images})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"code": http.StatusInternalServerError, "msg": fmt.Sprintf("发表动态失败: %v", err.Error())})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "发表动态成功", "data": resp})
}

// 获取好友动态
func GetFriendActivities(c *gin.Context) {
	var req request.GetFriendActivities
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": http.StatusBadRequest, "msg": fmt.Sprintf("参数错误: %v", err.Error())})
		return
	}
	resp, err := config.UserClient.GetFriendActivities(context.Background(), &userpb.GetFriendActivitiesReq{UserId: req.UserId, Page: req.Page, PageSize: req.PageSize})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"code": http.StatusInternalServerError, "msg": fmt.Sprintf("获取好友动态失败: %v", err.Error())})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "获取好友动态成功", "data": resp})
}

// ========== 好友系统 ==========

// 添加好友
func AddFriend(c *gin.Context) {
	var req request.AddFriend
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": http.StatusBadRequest, "msg": fmt.Sprintf("参数错误: %v", err.Error())})
		return
	}
	resp, err := config.UserClient.AddFriend(context.Background(), &userpb.AddFriendReq{
		UserId:       req.UserId,
		TargetUserId: req.TargetUserId,
		GroupId:      req.GroupId,
		Remark:       req.Remark,
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"code": http.StatusInternalServerError, "msg": fmt.Sprintf("添加好友失败: %v", err.Error())})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "添加好友成功", "data": gin.H{"success": resp.Success, "message": resp.Message}})
}

// 删除好友
func RemoveFriend(c *gin.Context) {
	var req request.RemoveFriend
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": http.StatusBadRequest, "msg": fmt.Sprintf("参数错误: %v", err.Error())})
		return
	}
	resp, err := config.UserClient.RemoveFriend(context.Background(), &userpb.RemoveFriendReq{UserId: req.UserId, TargetUserId: req.TargetUserId})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"code": http.StatusInternalServerError, "msg": fmt.Sprintf("删除好友失败: %v", err.Error())})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "删除好友成功", "data": gin.H{"success": resp.Success, "message": resp.Message}})
}

// 获取好友列表
func GetFriends(c *gin.Context) {
	var req request.GetFriends
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": http.StatusBadRequest, "msg": fmt.Sprintf("参数错误: %v", err.Error())})
		return
	}
	resp, err := config.UserClient.GetFriends(context.Background(), &userpb.GetFriendsReq{UserId: req.UserId})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"code": http.StatusInternalServerError, "msg": fmt.Sprintf("获取好友列表失败: %v", err.Error())})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "获取好友列表成功", "data": resp})
}

// 创建好友分组
func CreateFriendGroup(c *gin.Context) {
	var req request.CreateFriendGroup
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": http.StatusBadRequest, "msg": fmt.Sprintf("参数错误: %v", err.Error())})
		return
	}
	resp, err := config.UserClient.CreateFriendGroup(context.Background(), &userpb.CreateFriendGroupReq{UserId: req.UserId, GroupName: req.GroupName})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"code": http.StatusInternalServerError, "msg": fmt.Sprintf("创建分组失败: %v", err.Error())})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "创建分组成功", "data": resp})
}

// 重命名好友分组
func RenameFriendGroup(c *gin.Context) {
	var req request.RenameFriendGroup
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": http.StatusBadRequest, "msg": fmt.Sprintf("参数错误: %v", err.Error())})
		return
	}
	resp, err := config.UserClient.RenameFriendGroup(context.Background(), &userpb.RenameFriendGroupReq{UserId: req.UserId, GroupId: req.GroupId, GroupName: req.GroupName})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"code": http.StatusInternalServerError, "msg": fmt.Sprintf("重命名分组失败: %v", err.Error())})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "重命名分组成功", "data": resp})
}

// 删除好友分组
func DeleteFriendGroup(c *gin.Context) {
	var req request.DeleteFriendGroup
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": http.StatusBadRequest, "msg": fmt.Sprintf("参数错误: %v", err.Error())})
		return
	}
	resp, err := config.UserClient.DeleteFriendGroup(context.Background(), &userpb.DeleteFriendGroupReq{UserId: req.UserId, GroupId: req.GroupId})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"code": http.StatusInternalServerError, "msg": fmt.Sprintf("删除分组失败: %v", err.Error())})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "删除分组成功", "data": resp})
}

// 移动好友到分组
func MoveFriendToGroup(c *gin.Context) {
	var req request.MoveFriendToGroup
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": http.StatusBadRequest, "msg": fmt.Sprintf("参数错误: %v", err.Error())})
		return
	}
	resp, err := config.UserClient.MoveFriendToGroup(context.Background(), &userpb.MoveFriendToGroupReq{UserId: req.UserId, TargetUserId: req.TargetUserId, ToGroupId: req.ToGroupId})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"code": http.StatusInternalServerError, "msg": fmt.Sprintf("移动好友失败: %v", err.Error())})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "移动好友成功", "data": resp})
}
